package com.zz.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zz.personmodel.Employee;
import com.zz.employeedaoimpl.EmployeeDaoImpl;

@Controller
public class EmployeeController {
	@Autowired
	private EmployeeDaoImpl employeedaoimpl;
	private Employee e;
	@RequestMapping("/findemployeeView")
	public String findEmployeeList(
			@RequestParam(value = "identity_number", required = false) String identity_number,
			@RequestParam(value = "person_name", required = false) String person_name,
			@RequestParam(value = "birth_time", required = false) String birth_time,
			@RequestParam(value = "gender", required = false) String gender,
			@RequestParam(value = "page", required = false) Integer page,HttpServletRequest request,
			HttpServletResponse response,Model model) {
		String message="";//校验字符串
		ModelAndView mav = new ModelAndView();
		int size=employeedaoimpl.getperson(person_name, birth_time, gender);
		int pages=(size/4);//总頁數
		int pa=page/4;//当前页数
		model.addAttribute("pages", pages);
		model.addAttribute("pa", pa);
		if(size%4!=0) {
			size=(size-(size%4))+4;//数据条数不能整除的就减去余数加一页
		}
		if(size%4==0) {
			if(pa==pages){
				message="当前已经是最后一页";
				page=size-4;//减去多的一页
				model.addAttribute("message", message);
				model.addAttribute("page", page);
			}
		}
		if(page==0) {
			message="当前已经是第一页";
			model.addAttribute("message", message);
			page=0;
			model.addAttribute("page", page);
		}
		if(page==null) {
			message="欢迎光临";
			model.addAttribute("login", message);
		}else {
		if (identity_number == "" && ((person_name == "" && (birth_time == "" || gender == null))
				|| (birth_time == "" && gender == null))) {
			String findVerify = "不输入身份证号其他三项必须输入两项才能查询";
			request.setAttribute("findVerify", findVerify);
			mav.setViewName("findemployee");
		} else {
			List<Employee> list = employeedaoimpl.findEmployee(identity_number, person_name, birth_time,gender,page);
			if (list.isEmpty()) {
				String NullVerify = "抱歉查找的人不存在";
				request.setAttribute("NullVerify", NullVerify);
				model.addAttribute("NullVerify", NullVerify);
			} else {
				for(Employee e:list) {
					if(e.getGender()=="1") {
						e.setGender("男");
						list.add(e);
					}
					System.out.println(e.getGender());
					model.addAttribute("slist",list);
					break;
				}
				
			}
		}
		}
		model.addAttribute("person_name", person_name);
		model.addAttribute("gender", gender);
		model.addAttribute("identity_number", identity_number);
		model.addAttribute("birth_time", birth_time);
		model.addAttribute("page", page);
		return "findemployee";
	}

	@RequestMapping("/addemployee")
	public ModelAndView addEmployee(@RequestParam(value = "identity_number", required = false) String identity_number,
			@RequestParam(value = "person_name", required = false) String person_name,
			@RequestParam(value = "birth_time", required = false) String birth_time,
			@RequestParam(value = "sex") String sex, @RequestParam(value = "matital_status") String matital_status,
			@RequestParam(value = "spouse_name") String spouse_name, HttpServletRequest request,
			HttpServletResponse response) {
		String b = employeedaoimpl.addEmployee(identity_number, person_name, birth_time, sex, matital_status,
				spouse_name);
		request.setAttribute("addVerify", b);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("findemployee");
		return mav;
	}

	@RequestMapping("/deleteemployee")
	public ModelAndView deletEmployee(Model model, @RequestParam(value = "id", required = false) Integer id,
			HttpServletRequest request, HttpServletResponse response) {
		boolean b = employeedaoimpl.deletEmployee(id);
		String info;
		if (b) {
			info = "删除成功!";
		} else {
			info = "删除失败!";
		}
		request.setAttribute("deleteVerify", info);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("findemployee");
		return mav;
	}

	@RequestMapping("/updateemployee")
	public ModelAndView updateEmployee(Model model, @RequestParam(value = "id", required = false) Integer id,
			@RequestParam(value = "identity_number", required = false) String identity_number,
			@RequestParam(value = "person_name", required = false) String person_name,
			@RequestParam(value = "birth_time", required = false) String birth_time,
			@RequestParam(value = "sex", required = false) String sex,
			@RequestParam(value = "matital_status", required = false) String matital_status,
			@RequestParam(value = "spouse_name", required = false) String spouse_name,
			@RequestParam(value = "page", required = false) Integer page,HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		String b = employeedaoimpl.updateEmployee(id, identity_number, person_name, birth_time, sex, matital_status,
				spouse_name);
		request.setAttribute("updateVerify", b);
		List<Employee> list=employeedaoimpl.findEmployee(identity_number, person_name, birth_time, sex,page);
		mav.addObject("slist", list);
		mav.setViewName("findemployee");
		System.out.println(id);
		return mav;
	}
	@RequestMapping("selectpage")
	public ModelAndView selectpage(@RequestParam(value = "identity_number", required = false) String identity_number,
			@RequestParam(value = "person_name", required = false) String person_name,
			@RequestParam(value = "birth_time", required = false) String birth_time,
			@RequestParam(value = "gender", required = false) String gender,
			@RequestParam(value = "page", required = false) Integer page) {
		ModelAndView mav=new ModelAndView();
		List<Employee> list=employeedaoimpl.findEmployee(identity_number, person_name, birth_time, gender, page);
		mav.addObject("slist", list);
		mav.setViewName("findemployee");
		return mav;
	}
}